package com.sqx.modules.log.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.Result;
import com.sqx.modules.log.dao.SysLogLoginDao;
import com.sqx.modules.log.entity.SysLogLoginEntity;
import com.sqx.modules.log.service.SysLogLoginService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 登录日志
 *
 * @author fang
 *
 */
@Service
public class SysLogLoginServiceImpl extends ServiceImpl<SysLogLoginDao, SysLogLoginEntity> implements SysLogLoginService {

    @Override
    public Result page(JSONObject params) {
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<SysLogLoginEntity> sysLogLoginEntities = baseMapper.queryAll(params.getString("creatorName"), params.getString("ip"),
                params.getInteger("operation"),params.getInteger("status"));
        PageInfo<SysLogLoginEntity> sysLogErrorEntitiesPageInfo = new PageInfo<>(sysLogLoginEntities);
        return Result.success().put("data",sysLogErrorEntitiesPageInfo);
    }

    @Override
    public List<SysLogLoginEntity> list(Map<String, Object> params) {
        List<SysLogLoginEntity> entityList = baseMapper.selectList(getWrapper(params));

        return entityList;
    }

    private QueryWrapper<SysLogLoginEntity> getWrapper(Map<String, Object> params){
        String status = (String) params.get("status");
        String creatorName = (String) params.get("creatorName");

        QueryWrapper<SysLogLoginEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(status), "status", status);
        wrapper.like(StrUtil.isNotBlank(creatorName), "creator_name", creatorName);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSysLogLogin(SysLogLoginEntity entity) {
        baseMapper.insert(entity);
    }

}